package com.polaris.live.ui.message

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.polaris.live.adapter.MessageGreetAdapter
import com.polaris.live.common.R
import com.polaris.live.common.constant.PagConst.PAGE_CHANGE_LOADING_PAG
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityGreetBinding
import com.polaris.live.im.sfs.MESSAGE_GREET_0
import com.polaris.live.im.sfs.MESSAGE_GREET_1
import com.polaris.live.im.sfs.MESSAGE_GREET_2
import com.polaris.live.im.sfs.MESSAGE_GREET_3
import com.polaris.live.im.sfs.bean.chat.GreetMessage
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.MessageGreetViewModel

/**
 * GreetActivity
 * 打招呼
 *
 * @author Created by 半仙 on 2023/10/25/013 11:02
 */
class MessageGreetActivity : BaseVbActivity<MessageGreetViewModel, ActivityGreetBinding>() {

    private val mAdapter: MessageGreetAdapter by lazy { MessageGreetAdapter() }

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setNavigationBarColor(R.color.color_f8f9fa)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }

        mBinding.loadingPag.apply {
            path = PAGE_CHANGE_LOADING_PAG
            setRepeatCount(-1)
            play()
        }

        initObsever()

        mAdapter.setOnItemClickListener { _, _, position ->
            when (mAdapter.data[position].auditStatus) {
                MESSAGE_GREET_0 -> {//审核中
                    Router.instance.setPath(RoutePath.MESSAGE_UPLOAD_GREET_ACTIVITY)
                        .withInt("type", MESSAGE_GREET_0)
                        .withString("photoUrl", mAdapter.data[position].photoUrl)
                        .withString("content", mAdapter.data[position].content)
                        .navigation(this)
                }

                MESSAGE_GREET_1 -> {//审核成功
                    Router.instance.setPath(RoutePath.MESSAGE_UPLOAD_GREET_ACTIVITY)
                        .withInt("type", MESSAGE_GREET_1)
                        .withInt("id", mAdapter.data[position].greetTemplateId)
                        .withString("photoUrl", mAdapter.data[position].photoUrl)
                        .withString("content", mAdapter.data[position].content)
                        .navigation(this)
                }

                MESSAGE_GREET_2 -> {//审核失败
                    Router.instance.setPath(RoutePath.MESSAGE_UPLOAD_GREET_ACTIVITY)
                        .withInt("type", MESSAGE_GREET_2)
                        .withInt("id", mAdapter.data[position].greetTemplateId)
                        .withString("photoUrl", mAdapter.data[position].photoUrl)
                        .withString("content", mAdapter.data[position].content)
                        .navigation(this)
                }

                MESSAGE_GREET_3 -> {//上传模板
                    Router.instance.setPath(RoutePath.MESSAGE_UPLOAD_GREET_ACTIVITY)
                        .withInt("type", MESSAGE_GREET_3)
                        .navigation(this)
                }
            }
        }

    }

    private fun initObsever() {
        mViewModel.greetListLiveData.observe(this) { resultState ->
            mBinding.groupLoading.visibility = View.GONE
            parseState(resultState, {
                if (it.size < 3) {
                    val list = mutableListOf<GreetMessage>()
                    list.addAll(it)
                    list.add(GreetMessage(auditStatus = 3))
                    mAdapter.setDataSize(list.size - 1)
                    mAdapter.setList(list)
                } else {
                    mAdapter.setList(it)
                }
            }, {
                mBinding.errorView.visibility = View.VISIBLE
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getGreetList()
    }

}